package com.java.actual.cleanMine;



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 扫雷游戏主界面
 * @author tony.tang
 *
 */
public class CleanMine extends JFrame implements ActionListener{
	private JLabel text1,text2;
	public static JLabel remainMine;//剩余地雷数
	private JLabel time;//消耗时间
	private JButton reset;//重新开始
	private JPanel center;
	private int row,col,mine;
	
	
	public CleanMine(){
		text1=new JLabel("剩余地雷：");
		text2=new JLabel("消耗时间：");
		remainMine=new JLabel("10");
		time=new JLabel("0");
		reset=new JButton("重新开始");
		reset.addActionListener(this);
		JMenuBar bar=new JMenuBar();
		JMenu game=new JMenu("游戏");
		JMenu help=new JMenu("帮助");
		JMenuItem item;
		game.add(item=new JMenuItem("开局"));item.addActionListener(this);
		game.addSeparator();
		ButtonGroup bg=new ButtonGroup();
		game.add(item=new JCheckBoxMenuItem("初级",true));bg.add(item);item.addActionListener(this);
		game.add(item=new JCheckBoxMenuItem("中级"));bg.add(item);item.addActionListener(this);
		game.add(item=new JCheckBoxMenuItem("高级"));bg.add(item);item.addActionListener(this);
		game.add(item=new JCheckBoxMenuItem("自定义..."));bg.add(item);item.addActionListener(this);
		game.addSeparator();
		game.add(item=new JMenuItem("退出"));item.addActionListener(this);
	
		help.add(item=new JMenuItem("查看帮助"));item.addActionListener(this);
		help.add(item=new JMenuItem("关于扫雷..."));item.addActionListener(this);
		
		bar.add(game);
		bar.add(help);
		
		this.setJMenuBar(bar);
		
		init();
	}
	
	private void init(){
		JPanel north=new JPanel();
		north.add(text1);
		north.add(remainMine);
		north.add(reset);
		north.add(text2);
		north.add(time);
		
		this.add(north,BorderLayout.NORTH);
		this.row=9;
		this.col=9;
		this.mine=10;
		restart();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		new Thread(){
			public void run(){
				while(Integer.parseInt(remainMine.getText())>0){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					time.setText((Integer.parseInt(time.getText())+1)+"");
				}
			}
		}.start();
	}
	
	
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("初级")){
			this.row=9;
			this.col=9;
			this.mine=10;
			restart();
			return;
		}
		if(e.getActionCommand().equals("中级")){
			this.row=16;
			this.col=16;
			this.mine=40;
			restart();
			return;
		}
		if(e.getActionCommand().equals("高级")){
			this.row=16;
			this.col=30;
			this.mine=99;
			restart();
			return;
		}
		if(e.getActionCommand().equals("重新开始")){
			restart();
			return;
		}
	}

	private void restart(){
		if(center!=null){
			this.remove(center);
		}
		center=new AllButtonPanel(row,col,mine);
		this.add(center,BorderLayout.CENTER);
		this.remainMine.setText(mine+"");
		this.time.setText("0");
		this.setSize(col*30,row*30+10);
		this.setResizable(false);
		this.setVisible(true);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new CleanMine();

	}

}
